package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public Cart save(Cart cart) {
        //1. 判断当前cart在数据表中是否存在(查询条件: 套餐/菜品id  UserId)
        //select * from shopping_cart where dish_id = #{菜品id} and userId =#{登录用户id}
        //select * from shopping_cart where setmeal_id = #{套餐id} and userId =#{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null,Cart::getDishId,cart.getDishId())
                .eq(cart.getSetmealId() != null,Cart::getSetmealId,cart.getSetmealId())
                .eq(Cart::getUserId, UserHolder.get().getId());
        //返回一条记录
        Cart cartFromDB = cartMapper.selectOne(wrapper);

        if(cartFromDB != null){
            cartFromDB.setNumber(cartFromDB.getNumber()+1);
            cartMapper.updateById(cartFromDB);
            return cartFromDB;
        }else {
            cart.setUserId(UserHolder.get().getId());
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
            return cart;
        }
    }

    @Override
    public List<Cart> findList() {
        //查询当前用户的购物车
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        //select * from shopping_cart where user_id = #{登录用户id}
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        return cartMapper.selectList(wrapper);
    }

    @Override
    public Cart update(Cart cart) {
        //select * from shopping_cart where cart_dishId = #{菜品id} and cart_userId = #{登录用户}
        //select * from shopping_cart where cart_setmealId = #{套餐id} and cart_userId = #{登录用户}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null,Cart::getDishId,cart.getDishId())
                .eq(cart.getSetmealId() != null,Cart::getSetmealId,cart.getSetmealId())
                .eq(Cart::getUserId,UserHolder.get().getId());
        Cart cartFromDB = cartMapper.selectOne(wrapper);
        cartFromDB.setNumber(cartFromDB.getNumber()-1);
        if(cartFromDB.getNumber() != 0){
            cartMapper.updateById(cartFromDB);
            return cartFromDB;
        }else {
           cartMapper.delete(wrapper);
           return cart;
        }
    }

    @Override
    public void clean() {
        //delete from shopping_cart where user_id = #{登录用户}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }
}
